package com.zlt.appparking.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zlt.appparking.dto.ParkingLotDTO;
import com.zlt.appparking.dto.PersonLocationDTO;
import com.zlt.appparking.entity.ParkingLot;
import com.zlt.appparking.entity.ParkingLotExample;
import com.zlt.appparking.mapper.ParkingLotMapper;
import com.zlt.appparking.service.ParkingLotService;
import com.zlt.appparking.utils.MapUtil;
import com.zlt.appparking.vo.ParkingLotVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ParkingLotServiceImpl implements ParkingLotService {
    public  static String key="de45141709c9b22b708581065605fa0f";
    @Autowired
    private ParkingLotMapper parkingLotMapper;
    @Override
    public List<ParkingLotVO> getParks(PersonLocationDTO personLocationDTO) {
            //通过经纬度查询出所在地址，缩小到市再去查询范围内的停车场
            try {
                ParkingLotExample parkingLotExample=null;
                if(personLocationDTO.getPlAddress()==null&&personLocationDTO.getPlLongitude()!=null&&personLocationDTO.getPlLatitude()!=null) {
                    String address = MapUtil.getProvinceAndCity(personLocationDTO.getPlLongitude(), personLocationDTO.getPlLatitude(), key);
                    parkingLotExample = new ParkingLotExample();
                    parkingLotExample.createCriteria().andPlStateEqualTo((byte) 0);
                    parkingLotExample.createCriteria().andPlAddressLike("%" + address.toString() + "%");
                    List<ParkingLot> parkingLots = parkingLotMapper.selectByExample(parkingLotExample);
                    List<ParkingLotVO> parkingLotVOS=new ArrayList<>();
                    StringBuilder endLonLat=new StringBuilder();
                    endLonLat.append(personLocationDTO.getPlLongitude()+",");
                    endLonLat.append(personLocationDTO.getPlLatitude());
                    parkingLots.forEach(parkingLot -> {
                        StringBuilder startLonLat=new StringBuilder();
                        startLonLat.append(parkingLot.getPlLongitude()+",");
                        startLonLat.append(parkingLot.getPlLatitude());
                        Long distance1 = MapUtil.getDistance(startLonLat.toString(), endLonLat.toString(), key);
                        if(distance1!=null&&distance1<=personLocationDTO.getDistance()){
                            ParkingLotVO parkingLotVO=new ParkingLotVO();
                            parkingLotVO.setDistance(distance1);
                            BeanUtils.copyProperties(parkingLot,parkingLotVO);
                            parkingLotVOS.add(parkingLotVO);
                        }
                    });
                    return parkingLotVOS;
                }
                //按照地址去查
                if(personLocationDTO.getPlAddress()!=null){
                    parkingLotExample = new ParkingLotExample();
                    parkingLotExample.createCriteria().andPlStateEqualTo((byte) 0);
                    parkingLotExample.createCriteria().andPlAddressLike("%" +personLocationDTO.getPlAddress()+ "%");
                    List<ParkingLot> parkingLots = parkingLotMapper.selectByExample(parkingLotExample);
                    List<ParkingLotVO>parkingLotVOS=new ArrayList<>();
                    parkingLots.forEach(parkingLot -> {
                        ParkingLotVO parkingLotVO=new ParkingLotVO();
                        BeanUtils.copyProperties(parkingLot,parkingLotVO);
                        parkingLotVOS.add(parkingLotVO);
                    });
                    return parkingLotVOS;
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("查询失败");
            }
    return null;
    }
}
